package com.focusai.efairy.ui.activity;

import android.Manifest;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.focusai.efairy.EFApplication;
import com.focusai.efairy.R;
import com.focusai.efairy.business.update.AppUpdateAgent;
import com.focusai.efairy.model.manager.CacheManager;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;
import com.focusai.efairy.utils.ActivityUtils;
import com.focusai.efairy.utils.permission.PermissionListener;
import com.focusai.efairy.utils.permission.PermissionUtil;

import java.util.List;

/**
 * Created by luo_guangwen on 2017/8/13.
 */

public class SettingActivity extends SwipeBackBaseActivity implements View.OnClickListener{
    public final static int REQUESTCODE_SETTING = 0x11;
    private SwitchCompat switchCompat;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_setting);
        initView();
    }

    @Override
    public void initTitte() {
        getCenterTextView().setText(R.string.set);
    }

    @Override
    protected void initView() {
        findView(R.id.btn_login_out).setOnClickListener(this);
        findView(R.id.ll_check_app).setOnClickListener(this);
        switchCompat = findView(R.id.switch_report_item);
        TextView textView  = findView(R.id.tv_version_name);
        TextView text_app_name  = findView(R.id.text_app_name);
        textView.setText(ActivityUtils.getVersionName());
        switchCompat.setChecked(CacheManager.getUserMsgTip());
        switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                CacheManager.setUserMsgTip(isChecked);
            }
        });
        if (EFApplication.isEfairyApp()){
            text_app_name.setText(R.string.app_name);
        }else if (EFApplication.isCciotApp()){
            text_app_name.setText(R.string.app_name_cciot);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login_out:
                showLogoutDialog();
                break;
            case R.id.ll_check_app:
                checkPermission();
                AppUpdateAgent.checkAppUpdate(SettingActivity.this,true);
                break;
            default:
                break;
        }
    }

    private void loginOut(){
        ActivityUtils.cleanAndStartLoginActivity(SettingActivity.this);
        setResult(RESULT_OK);
        finish();
    }

    private void showLogoutDialog(){
        AlertDialog dialog = new AlertDialog.Builder(getContext()).create();
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.sure), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                loginOut();
            }
        });
        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.cancal), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });

        dialog.setCancelable(true);
        dialog.setMessage(getString(R.string.make_sure_loginout));
        dialog.show();
    }
    private void checkPermission(){
        checkPermission(PermissionUtil.PERMISSION_STORAGE, new PermissionListener() {
            @Override
            public void onSucceed(int requestCode, List<String> grantedList) {

            }

            @Override
            public void onFailed(int requestCode, List<String> deniedList) {
                showToast("应用没写SD卡权限");
            }
        }, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }
}
